/* ====================================== toast  ====================================== */
import React from 'react'
import { Switch } from 'antd'
// ===================================================================== Select
class Index extends React.Component {
	state = {
		
	}
	
	onChange = value => {
		const { onChange, onChanged, name, isBool, disValue, outer } = this.props
		if( typeof disValue === 'boolean' && value === !disValue) return;
		
		if(!outer){ this.setState({ value }) }
		
		if(!isBool){ value = value ? 1 : 0 }
		const model = name ? { [name]: value } : value
		onChanged?.({ model, value, name } )
		
		onChange?.(value)
	}
	
	setValue = value => this.setState({ value })
	getValue = () => this.state.value
	clear = () => this.setValue(false)
	reset = () => this.setState({ value: undefined })
	
	render(){
		const  { size, disabled, onClick, loading } = this.props
		let value = this.state.value === undefined ? this.props.value : this.state.value
		value = +value === 1 || value === true ? true : false
		
		return (
			<div onClick			= { e => { e.stopPropagation(); onClick && onClick()} }>
				<Switch
					size			= { size || 'middel' } 
					onChange		= { onClick ? null : this.onChange }
					checked		 	= { value }
					disabled		= { disabled }
					loading			= { loading }
				/>
			</div>
		)
	}
}
export default Index